<?php
/**
 * 门店
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:39
 */
namespace Model;
class StoreMod extends Mod
{
	public function __construct()
	{
		$this->_table = 'kuyuan_vbz_store';
	}

    /**
     * APP端领券页面获取门店卡券列表
     * @param $_activity_id 活动id
     * @param $_limit  请求下标
     * @param $_sell_range  搜索门店经营范围数组1，2，3
     * @return Array 返回数据
     */
	public function activityGetCoupon($_activity_id,Array $_limit,Array $_sell_range=array())
    {
        global $_W;
        $_where = "
                        A.uniacid = :uniacid
                    AND
                        A.status = :store_status
                    AND
                        B.activity_id = :activity_id
                    AND
                        B.goal = :goal
                    AND
                        B.status = :coupon_status
                    AND
                        C.status = :store_coupon_status
                  ";
        if(count($_sell_range) > 0){
              $_where .= "AND (";
              foreach($_sell_range as $_v){
                      $_where .= " FIND_IN_SET(".$_v.",A.sell_range) OR ";
              }
              $_where = substr($_where,0,-3);
              $_where .= ") ";
        }
        $_bind = array('uniacid'=>$_W['account']['uniacid'],'activity_id'=>$_activity_id,'goal'=>2,'coupon_status'=>1,'store_status'=>1,'store_coupon_status'=>1);
        $_sql = "
                 SELECT 
                        A.id as store_id,A.name as store_name,A.logo
                 FROM
                      ".tablename($this->_table)." as A
                 LEFT JOIN
                      ".tablename('kuyuan_vbz_activity_coupon')." as B
                 ON
                      A.id = B.store_id
                 RIGHT JOIN
                      ".tablename('kuyuan_vbz_store_coupon')." as C
                 ON
                      B.coupon_id = C.id                      
                 WHERE
                      ".$_where."
                 GROUP BY
                      A.id
                 ORDER BY
                      A.id ASC
                 LIMIT
                    {$_limit[0]},{$_limit[1]} 
                ";
        return pdo_fetchall($_sql,$_bind);
    }

    /**
     * 获取门店
     * @param $_store_id 门店id
     * @param $_activity_id 活动id
     * @return Array 返回数据
     */
    public function activityStoreGetCoupon($_store_id,$_activity_id)
    {
        $_where = "
                        A.store_id = :store_id
                    AND
                        A.activity_id = :activity_id
                    AND
                        A.goal = :goal      
                    AND
                        A.status = :put_status 
                    AND
                        B.status = :coupon_status        
                  ";
        $_bind = array('store_id'=>$_store_id,'activity_id'=>$_activity_id,'goal'=>2,'put_status'=>1,'coupon_status'=>1);
        $_sql = "   
                 SELECT
                        A.id as treasure_id,A.number,A.give_number,A.user_number,A.use_limit_type,A.limit_time,B.name,B.images,B.cost,B.type
                  FROM
                        ".tablename('kuyuan_vbz_activity_coupon')." as A
                  LEFT JOIN
                        ".tablename('kuyuan_vbz_store_coupon')." as B
                  ON
                        A.coupon_id = B.id
                  WHERE
                        ".$_where."
                  ORDER BY
                        A.give_number < A.number DESC,A.id ASC
                 ";
                 return pdo_fetchall($_sql,$_bind);
    }


}